package cmd

import (
	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/gscore/server"
	"gitee.com/crack007/proxy/src/config"
	"gitee.com/crack007/proxy/src/proxy/handler"
	"github.com/spf13/cobra"
)

func NewMysqlProxyCommand() *goose.ModuleBuilder {
	sessionHandler := handler.NewMysqlSessionHandler()
	sessionBuilder := server.NewSessionBuilder(sessionHandler)
	sessionBuilder.SetReadBuffer(0xffff)
	serverBuilder := server.NewServerBuilder(handler.NewServerHandler(), sessionBuilder)
	serverBuilder.EnablePortFlag(3306).EnableDebugFlag().EnableExecConfigFlag()
	serverBuilder.SetServerName("mysqlProxy")
	serverBuilder.RegisterFlag(func(cobraCmd *cobra.Command) {
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.Slow, "slow", 0, "设置慢查询的时间(单位：毫秒)。例如超过500毫秒为慢查询，则 --slow 500")
		cobraCmd.PersistentFlags().BoolVar(&config.MysqlConfig.ShowCostTime, "show-cost-time", false, "显示客户端命令的执行耗时")
		cobraCmd.PersistentFlags().Uint16Var(&sessionHandler.Port, "proxy-port", 3306, "代理目标端口")
		cobraCmd.PersistentFlags().StringVar(&sessionHandler.Host, "proxy-host", "127.0.0.1", "代理目标 IP")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitInterval, "split-interval", 0, "打印日志分割符的时间间隔，0的话则不显示")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitCount, "split-count", 30, "日志分割符数量")
		cobraCmd.PersistentFlags().StringVar(&config.MysqlConfig.SplitTag, "split-tag", "#", "日志分割符")
		cobraCmd.PersistentFlags().Uint16Var(&serverBuilder.PprofPort, "pprof", 0, "pprof 监听端口,如果为 0 则不开启")
		cobraCmd.PersistentFlags().Uint16Var(&config.LogServerConfig.Port, "log-port", 0, "日志服务监听端口,如果为 0 则不开启")
		cobraCmd.PersistentFlags().IntVar(&config.LogServerConfig.Buffer, "log-buffer", 0xff, "日志服务缓冲数量")
	})

	return goose.NewModuleBuilder(serverBuilder).SetName("mysql").SetDesc("mysql协议代理")
}

func NewTcpProxyCommand() *goose.ModuleBuilder {
	sessionHandler := handler.NewTcpSessionHandler()
	sessionBuilder := server.NewSessionBuilder(sessionHandler)
	sessionBuilder.SetReadBuffer(0xffff)
	serverBuilder := server.NewServerBuilder(handler.NewServerHandler(), sessionBuilder)
	serverBuilder.EnablePortFlag(10020).EnableDebugFlag()
	serverBuilder.SetServerName("tcpProxy")

	serverBuilder.RegisterFlag(func(cobraCmd *cobra.Command) {
		cobraCmd.PersistentFlags().Uint16Var(&sessionHandler.Port, "proxy-port", 10020, "代理目标端口")
		cobraCmd.PersistentFlags().StringVar(&sessionHandler.Host, "proxy-host", "127.0.0.1", "代理目标 IP")
		cobraCmd.PersistentFlags().StringVar(&sessionHandler.Output, "output", "hex-dump", "输出数据包格式(hex-dump: hex+ASCII格式, text: ASCII格式, hex-stream: 十六进制文本), 多种输出方式使用 | 隔开,例如 hex-dump|text")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitInterval, "split-interval", 0, "打印日志分割符的时间间隔，0的话则不显示")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitCount, "split-count", 30, "日志分割符数量")
		cobraCmd.PersistentFlags().StringVar(&config.MysqlConfig.SplitTag, "split-tag", "#", "日志分割符")
		cobraCmd.PersistentFlags().Uint16Var(&config.LogServerConfig.Port, "log-port", 0, "日志服监听端口,如果为 0 则不开启")
		cobraCmd.PersistentFlags().IntVar(&config.LogServerConfig.Buffer, "log-buffer", 0xff, "日志服务缓冲数量")
	})

	return goose.NewModuleBuilder(serverBuilder).SetName("tcp").SetDesc("tcp协议代理")
}

func NewRedisProxyCommand() *goose.ModuleBuilder {
	sessionHandler := handler.NewRedisSessionHandler()
	sessionBuilder := server.NewSessionBuilder(sessionHandler)
	sessionBuilder.SetReadBuffer(0xffff)
	serverBuilder := server.NewServerBuilder(handler.NewServerHandler(), sessionBuilder)
	serverBuilder.EnablePortFlag(6379).EnableDebugFlag().EnableExecConfigFlag()
	serverBuilder.SetServerName("redisProxy")
	serverBuilder.RegisterFlag(func(cobraCmd *cobra.Command) {
		cobraCmd.PersistentFlags().IntVar(&config.RedisConfig.Slow, "slow", 0, "设置慢查询的时间(单位：毫秒)。例如超过500毫秒为慢查询，则 --slow 500")
		cobraCmd.PersistentFlags().BoolVar(&config.RedisConfig.ShowCostTime, "show-cost-time", false, "显示客户端命令的执行耗时")
		cobraCmd.PersistentFlags().Uint16Var(&sessionHandler.Port, "proxy-port", 6379, "代理目标端口")
		cobraCmd.PersistentFlags().StringVar(&sessionHandler.Host, "proxy-host", "127.0.0.1", "代理目标 IP")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitInterval, "split-interval", 0, "打印日志分割符的时间间隔，0的话则不显示")
		cobraCmd.PersistentFlags().IntVar(&config.MysqlConfig.SplitCount, "split-count", 30, "日志分割符数量")
		cobraCmd.PersistentFlags().StringVar(&config.MysqlConfig.SplitTag, "split-tag", "#", "日志分割符")
		cobraCmd.PersistentFlags().StringVar(&config.RedisConfig.Exclude, "exclude", "", "需要排除的 redis 指令，多个用逗号 , 分开")
		cobraCmd.PersistentFlags().Uint16Var(&serverBuilder.PprofPort, "pprof", 0, "pprof 监听端口, 如果为 0 则不开启")
		cobraCmd.PersistentFlags().Uint16Var(&config.LogServerConfig.Port, "log-port", 0, "日志服监听端口,如果为 0 则不开启")
		cobraCmd.PersistentFlags().IntVar(&config.LogServerConfig.Buffer, "log-buffer", 0xff, "日志服务缓冲数量")
	})

	return goose.NewModuleBuilder(serverBuilder).SetName("redis").SetDesc("redis协议代理")
}
